package com.rs.controller;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.github.pagehelper.Page;
import com.rs.model.ZhClasses;
import com.rs.service.ZhClassesService;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

public class ClassList extends JInternalFrame {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClassList frame = new ClassList(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private Object [][]data=null;
	
	private String head[]=null;
	
	//所有的部门信息
	private Page<ZhClasses> dataList;
	
	//首页
	private Integer pageNum =1;
	
	JDesktopPane desktopPane_1;
	
	//总页数
	private Integer pages;

	private JTable table;

	
	
	private ZhClassesService zhClassesService = SpringContextUtils.getBean(ZhClassesService.class);
	
	private JTextField textField;

	/**
	 * Create the frame.
	 */
	public ClassList(final JDesktopPane desktopPane_1,final Integer user) {
		this.desktopPane_1 = desktopPane_1;
		setClosable(true);
		setMaximizable(true);
		setIconifiable(true);
		setResizable(false);
		setTitle("班级毕业审核列表");
		setBounds(100, 100, 935, 472);
		getContentPane().setLayout(null);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 67, 919, 276);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
		tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样
		table.setDefaultRenderer(Object.class, tcr);
		head=new String[] {
				"编号", "教师姓名", "开班时间", "结业时间", "班级名称", "班级人数", "审批人", "审核状态"
		        };
		
		table.setModel(new DefaultTableModel(queryData(pageNum, new ZhClasses()),head));
		scrollPane.setViewportView(table);
		table.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e) {
				int row =table.rowAtPoint(e.getPoint());// 选中行
				ZhClasses zhClasses = new ZhClasses();
				zhClasses.setClId((int)table.getValueAt(row, 0));
				zhClasses.setClUName((String)table.getValueAt(row, 1));
				zhClasses.setClTimeString((String)table.getValueAt(row, 2));
				zhClasses.setClEndtimeString((String)table.getValueAt(row, 3));
				zhClasses.setClName((String)table.getValueAt(row, 4));
				zhClasses.setClStudentnum((int)table.getValueAt(row,5));
				zhClasses.setClShuname((String)table.getValueAt(row, 6));
				if(table.getValueAt(row, 7).equals("待审核")) {
					zhClasses.setClExamine(0);
				}else if(table.getValueAt(row, 7).equals("已通过")) {
					zhClasses.setClExamine(1);
				}else {
					zhClasses.setClExamine(2);
				}
				zhClasses.setClShuid(user);
				System.out.println(zhClasses.getClExamine());
				doDefaultCloseAction();
				new ClassListUpdate(zhClasses,desktopPane_1).setVisible(true);
			}
		});
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 919, 68);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel("班级名称：");
		label.setBounds(67, 18, 71, 21);
		panel.add(label);
		
		JLabel label_1 = new JLabel("审核状态：");
		label_1.setBounds(297, 21, 78, 15);
		panel.add(label_1);
		
		final JComboBox comboBox_2 = new JComboBox();
		comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"无", "已通过", "待审核", "未通过"}));
		comboBox_2.setBounds(385, 18, 93, 21);
		panel.add(comboBox_2);
	
		
		textField = new JTextField();
		textField.setBounds(148, 18, 93, 21);
		panel.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ZhClasses zhClasses = new ZhClasses();
				zhClasses.setClName(textField.getText());
				if(!comboBox_2.getSelectedItem().equals("无")){
					if(comboBox_2.getSelectedItem().equals("待审核")) {
						zhClasses.setClExamine(0);
					}else if(comboBox_2.getSelectedItem().equals("已通过")) {
						zhClasses.setClExamine(1);
					}else {
						zhClasses.setClExamine(2);
					}
				}
				SetTheValue(zhClasses);
			}
		});
		btnNewButton.setBounds(540, 17, 93, 23);
		panel.add(btnNewButton);
		
	
		
		
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(0, 341, 919, 102);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		JButton btnNewButton_1 = new JButton("首页");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageNum=1;
				ZhClasses zhClasses = new ZhClasses();
				zhClasses.setClName(textField.getText());
				if(!comboBox_2.getSelectedItem().equals("无")){
					if(comboBox_2.getSelectedItem().equals("待审核")) {
						zhClasses.setClExamine(0);
					}else if(comboBox_2.getSelectedItem().equals("已通过")) {
						zhClasses.setClExamine(1);
					}else {
						zhClasses.setClExamine(2);
					}
				}
				SetTheValue(zhClasses);
			}
		});
		btnNewButton_1.setBounds(56, 51, 93, 23);
		panel_1.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("上一页");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageNum-=1;
				// 判断是否是第一页				
				if(pageNum < 1) {
					showMessageDialog("已是第一页！");
					pageNum=1;
				}else {
					ZhClasses zhClasses = new ZhClasses();
					zhClasses.setClName(textField.getText());
					if(!comboBox_2.getSelectedItem().equals("无")){
						if(comboBox_2.getSelectedItem().equals("待审核")) {
							zhClasses.setClExamine(0);
						}else if(comboBox_2.getSelectedItem().equals("已通过")) {
							zhClasses.setClExamine(1);
						}else {
							zhClasses.setClExamine(2);
						}
					}
					SetTheValue(zhClasses);
				}
			}
		});
		btnNewButton_2.setBounds(499, 51, 93, 23);
		panel_1.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("下一页");
		btnNewButton_3.addActionListener(new ActionListener() {
			
			
			
			public void actionPerformed(ActionEvent e) {
				// 判断是否是最后一页				
				if(pageNum == pages) {
					showMessageDialog("已是最后一页！");
				}else {
					pageNum+=1;
					ZhClasses zhClasses = new ZhClasses();
					zhClasses.setClName(textField.getText());
					if(!comboBox_2.getSelectedItem().equals("无")){
						if(comboBox_2.getSelectedItem().equals("待审核")) {
							zhClasses.setClExamine(0);
						}else if(comboBox_2.getSelectedItem().equals("已通过")) {
							zhClasses.setClExamine(1);
						}else {
							zhClasses.setClExamine(2);
						}
					}
					SetTheValue(zhClasses);
				}
			}
		});
		btnNewButton_3.setBounds(778, 51, 93, 23);
		panel_1.add(btnNewButton_3);
	}
	
	//提示弹窗
	public void showMessageDialog(String massage){
		JOptionPane.showMessageDialog(this,massage);
	}
	
	//清空重新渲染数据
	public void SetTheValue(ZhClasses record){
		TableModel tb=table.getModel();
		listNull(tb,10,3);
		table.setModel(new DefaultTableModel(queryData(pageNum,record),head));
	}
	
	//清空数据
	public void listNull(TableModel tb,int row,int cou){
		 for(int i=0;i<row;i++){
			for(int j=0;j<cou;j++){
				tb.setValueAt(null, i, j);
			}
		 }
	}
	
	 //生成表格数据
    /**
     * @return
     */
    public Object[][] queryData(Integer pageNum,ZhClasses record){
        List<ZhClasses> list= getData(pageNum, record);
        data = new Object[10][head.length];
        for(int i=0;i<10;i++){
            for(int j=0;j<head.length;j++){
            	if(i>list.size()-1) {
            		 data[i][0]=null;
                     data[i][1]=null;
                     data[i][2]=null;
                     data[i][3]=null;
                     data[i][4]=null;
                     data[i][5]=null;
                     data[i][6]=null;
                     data[i][7]=null;
            	}else {
            		 data[i][0]=list.get(i).getClId();
                     data[i][1]=list.get(i).getClUName();
                     data[i][2]= new DateFormatUtil().getDateString(list.get(i).getClTime(),"yyyy-MM-dd");
                     data[i][3]=new DateFormatUtil().getDateString(list.get(i).getClEndtime(),"yyyy-MM-dd");
                     data[i][4]=list.get(i).getClName();
                     data[i][5]=list.get(i).getClStudentnum();
                     data[i][6]=list.get(i).getClShuname();
                     if(Optional.ofNullable(list.get(i).getClExamine()).isPresent()) {
                    	 if (list.get(i).getClExamine()  == 0) {
                       	  data[i][7]= "待审核";
	   					}else if(list.get(i).getClExamine()  == 1){
	   						  data[i][7]= "已通过";
	   					}else {
	   						data[i][7]= "未通过";
	   					}
                     }
            	}
            }
        }
        return data;
    }
	//获取数据
	public List<ZhClasses> getData(Integer pageNum,ZhClasses record){
		record.setPageNum(pageNum);
		 dataList =	zhClassesService.graduationZhClassessByEnd(record);
		pages = dataList.getPages();
		//获取数据 
		List<ZhClasses> data =	new ArrayList<ZhClasses>();
		for (ZhClasses zhClasses : dataList.getResult()) {
			data.add(zhClasses);
		}
		return data;
	}
}
